package cn.ondraw.ragflow.controller;

import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.service.DocumentService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/v1/datasets")
public class DocumentController {
    private final DocumentService documentService;

    /**
     * 上传文档
     *
     * @param datasetId 关联的数据集 ID
     * @param files     要上传的文件列表
     * @return 上传文档的响应对象
     */
    @PostMapping(value = "/{datasetId}/documents", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Object> uploadDocuments(@PathVariable String datasetId, @RequestParam("file") MultipartFile[] files) {
        try {
            return R.ok(documentService.uploadDocuments(datasetId, files));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新文档
     *
     * @param datasetId  关联的数据集 ID
     * @param documentId 要更新的文档 ID
     * @param request    更新文档的请求对象
     * @return 更新文档的响应对象
     */
    @PutMapping("/{datasetId}/documents/{documentId}")
    public R<Object> updateDocument(@PathVariable String datasetId, @PathVariable String documentId, @RequestBody UpdateDocumentRequest request) {
        try {
            return R.ok(documentService.updateDocument(datasetId, documentId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 下载文档
     *
     * @param datasetId  关联的数据集 ID
     * @param documentId 要下载的文档 ID
     * @return 文档内容的字节数组响应
     */
    @GetMapping("/{datasetId}/documents/{documentId}")
    public R<ResponseEntity<byte[]>> downloadDocument(@PathVariable String datasetId, @PathVariable String documentId) {
        try {
            return R.ok(documentService.downloadDocument(datasetId, documentId));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 列出文档
     *
     * @param request 列出文档的请求对象
     * @return 列出文档的响应对象
     */
    @GetMapping("/{datasetId}/documents")
    public R<Object> listDocuments(@PathVariable String datasetId, ListDocumentsRequest request) {
        try {

            request.setDataset_id(datasetId);
            return R.ok(documentService.listDocuments(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除文档
     *
     * @param datasetId 关联的数据集 ID
     * @param request   删除文档的请求对象
     * @return 删除文档的响应对象
     */
    @DeleteMapping("/{datasetId}/documents")
    public R<Object> deleteDocuments(@PathVariable String datasetId, @RequestBody DeleteDocumentsRequest request) {
        try {
            return R.ok(documentService.deleteDocuments(datasetId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 解析文档
     *
     * @param datasetId 数据集 ID
     * @param request   解析文档的请求对象
     * @return 解析文档的响应对象
     */
    @PostMapping("/{datasetId}/chunks")
    public R<Object> parseDocuments(@PathVariable String datasetId, @RequestBody ParseDocumentsRequest request) {
        try {
            return R.ok(documentService.parseDocuments(datasetId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 停止解析文档
     *
     * @param datasetId 关联的数据集 ID
     * @param request   停止解析文档的请求对象
     * @return 停止解析文档的响应对象
     */
    @DeleteMapping("/{datasetId}/chunks")
    public R<Object> stopParsingDocuments(@PathVariable String datasetId, @RequestBody StopParsingDocumentsRequest request) {
        try {
            return R.ok(documentService.stopParsingDocuments(datasetId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
